package com.starblink.android.basic.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView.ScaleType
import com.starblink.android.basic.databinding.ViewCircleLogoBinding
import com.starblink.android.basic.databinding.ViewRoundedRetangleLogoBinding
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.basic.ext.load
import com.starblink.basic.log.SKLogger
import com.starblink.basic.style.R
import com.starblink.basic.util.CommUtils

/**
 * 商品、店铺 logo 视图（兼容无url时展示名称首字母）
 */
class RoundedRectangleLogoView @JvmOverloads constructor(
    context: Context,
    attrSet: AttributeSet?,
    defStyleAttr: Int = 0,
) : FrameLayout(context, attrSet, defStyleAttr) {

    private val binding: ViewRoundedRetangleLogoBinding
    private val avatar = R.drawable.icon_store_default

    init {
        binding = ViewRoundedRetangleLogoBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray = context.obtainStyledAttributes(
            attrSet,
            R.styleable.RoundedRectangleLogoView
        )
        val webTextSize =
            typedArray.getDimensionPixelSize(
                R.styleable.RoundedRectangleLogoView_rrlv_webTextSize,
                14
            ).toFloat()
        val innerImgWH =
            typedArray.getDimensionPixelSize(
                R.styleable.RoundedRectangleLogoView_rrlv_inner_img_wh,
                0
            ).toFloat()

        val cornerRadius = typedArray.getDimensionPixelSize(
            R.styleable.RoundedRectangleLogoView_rrlv_corner_radius,
            0
        ).toFloat()

        val cornerInnerRadius = typedArray.getDimensionPixelSize(
            R.styleable.RoundedRectangleLogoView_rrlv_corner_inner_radius,
            0
        ).toFloat()

        if (cornerRadius > 0) {
            binding.ivContain.setCornerRadius(cornerRadius)
        }

        if (typedArray.hasValue(R.styleable.RoundedRectangleLogoView_rrlv_webBackground)) {
            val mBackgroundDrawable =
                typedArray.getDrawable(R.styleable.RoundedRectangleLogoView_rrlv_webBackground)
            binding.tvStore.background = mBackgroundDrawable
        }

        if (typedArray.hasValue(R.styleable.RoundedRectangleLogoView_rrlv_webTextColor)) {
            val color =
                typedArray.getColor(
                    R.styleable.RoundedRectangleLogoView_rrlv_webTextColor,
                    Color.parseColor("#FFFFFF")
                )
            binding.tvStore.setTextColor(color)
        }
        typedArray.recycle()
        binding.tvStore.setTextSize(TypedValue.COMPLEX_UNIT_PX, webTextSize)


        if (innerImgWH > 0) {
            val lParams = binding.ivStore.layoutParams
            lParams.width = innerImgWH.toInt()
            lParams.height = innerImgWH.toInt()
            binding.ivStore.layoutParams = lParams
        }




        if (cornerInnerRadius > 0) {
            binding.ivStore.enableCorner(cornerInnerRadius.toInt())
            binding.tvStore.enableCorner(cornerInnerRadius.toInt())
        } else {
            binding.ivStore.enableCorner(4.dp2px())
            binding.tvStore.enableCorner(4.dp2px())
        }

    }

    fun loadRect(
        url: String?,
        website: String?,
        scaleType: ScaleType = ScaleType.CENTER_INSIDE
    ) {
        binding.ivStore.scaleType = scaleType
        if (url?.isNotEmpty() == true) {
            binding.ivStore.load(url,
                placeholder = CommUtils.getDrawable(avatar), errorCall = {
                    binding.tvStore.text = website?.first().toString()
                    binding.tvStore.visibleOrGone(true) {}
                    binding.ivStore.visibleOrGone(false) {}
                })
        } else {
            if (website?.isNotEmpty() == true) {
                binding.tvStore.text = website.first().toString().uppercase()
                binding.tvStore.visibleOrGone(true) {}
                binding.ivStore.visibleOrGone(false) {}
            } else {
                binding.ivStore.setImageResource(avatar)
            }
        }
    }
}